<?php

/**
 * AlertController
 * 
 * @author
 * @version 
 */

class Merchant_AlertController extends Zend_Controller_Action {
	
	public function preDispatch(){
		$this->tplDirectory = "merchant/alert/";
	}
		
	public function indexAction() {
		
	}
	
	public function getAlertsAction(){

		$alert_level = $this->_request->getParam('level',2);
		$this->view->alert_level = $alert_level;
		
		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			if(!$customer_id)return false;
			$cond =array();
			$cond['customer_id'] = $customer_id;
		
			$alert_level = $this->_request->getParam('alert_level','');
			$dateFrom = $this->_request->getParam('dateFrom','');
			$dateTo = $this->_request->getParam('dateTo','');
			
			if($alert_level !==''){
				$cond['alert_level'] = $alert_level;
			}
			
			$dateFrom && $cond['dateFrom'] = $dateFrom;
			$dateTo && $cond['dateTo'] = $dateTo;
			
			$page = $this->_request->getParam('page',1);
			$pageSize = $this->_request->getParam('pageSize',20);
			$page = $page?$page:1;
			$pageSize = $pageSize?$pageSize:20;
			$data =array('ask'=>0,'total'=>0);
			$orderby = 'alert_id desc';
			$alertStatus=array('0','1');
			//unred Notice total
			$tmpcond = array('customer_id'=>$customer_id,'alert_status_in'=>$alertStatus,'alert_level'=>0);
			$noticeTotal = Merchant_Service_Alert::getByCond($tmpcond,'count(*)','',0,0);
			
			//unred Warning total
			$tmpcond = array('customer_id'=>$customer_id,'alert_status_in'=>$alertStatus,'alert_level'=>1);
			$warningTotal = Merchant_Service_Alert::getByCond($tmpcond,'count(*)','',0,0);
			
			//unred Important total
			$tmpcond = array('customer_id'=>$customer_id,'alert_status_in'=>$alertStatus,'alert_level'=>2);
			$importantTotal = Merchant_Service_Alert::getByCond($tmpcond,'count(*)','',0,0);
			
			$total = Merchant_Service_Alert::getByCond($cond,'count(*)','',0,0);
			$rows = Merchant_Service_Alert::getByCond($cond,'*',$orderby,$pageSize,($page-1)*$pageSize);
			
			if($total){
				$data['ask'] = 1;
				$data['total'] = $total;
				$data['result'] = $rows;				
			}
			$data['statusTotal'] = array(
							"notice" => $noticeTotal,
							"warning" => $warningTotal,
							"important" => $importantTotal );
			die(json_encode($data));
		}
		
		echo Cff::renderTpl($this->tplDirectory."alert-list.tpl","user-layout");
	}
	
	public function batchProcessAction(){
		$alert_status = $this->_request->getParam('alert_status','');
		$alert_ids = $this->_request->getParam('alert_ids','');		//alert_id 数组
		$checks = array();
		foreach ($alert_ids as $key => $val){
			if($alert_status === '') continue;
			$tmpArr = array('alert_status'=>$alert_status);
			$checks[] = Merchant_Service_Alert::update($tmpArr,$val,'alert_id');
		}
		$data = array('ask' => 0,'result' => array());
		if(count($checks)){
			$data['ask'] = 1;
			$data['result'] = $checks;
		}
		
		die(json_encode($data));
		
	}
	
	public function detailAction(){
		$alert_id = $this->_request->getParam('alert_id','');
		
		$row = Merchant_Service_Alert::getByValue($alert_id,'alert_id');
		$data = array('ask' => 0,'result' => array(),'change'=>0);
		if(count($row)){
			if($row['alert_status'] === '0'){
				$tmpArr = array('alert_status'=>1);		//未读 改为 已读
				$check = Merchant_Service_Alert::update($tmpArr,$alert_id,'alert_id');
				$check && $data['change'] = 1;
			}
			$data['ask'] = 1;
			$data['result'] = $row;
		}
		die(json_encode($data));
	}
	
	public function alertMessagesAction(){
		$alertLevelArr = Merchant_Service_Alert::$alertLevelArr;
		$colArr = Merchant_Service_AlertMessages::getColumnByValue('am_config_id','0','customer_id');
		
		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			if(!$customer_id)return false;
			
			$cond = $rows = array();
			$data = array('ask'=>0);
			$cond['customer_id'] = array('0',$customer_id);
			foreach ($colArr as $key => $val){
				$cond['am_config_id'] = $val['am_config_id'];
				$row = Merchant_Service_AlertMessages::getRowByCond($cond,'*','customer_id desc','row');
				$rows[] = $row;
			}
		
			foreach ($rows as $key => $val){
				$rows[$key]['send_level'] = $alertLevelArr[$val['am_level']];
			}
			if(count($rows)){
				$data['ask'] = 1;
				$data['result'] = $rows;	
			}
			die(json_encode($data));
		}
		
		echo Cff::renderTpl($this->tplDirectory."alert_messages.tpl","user-layout");
	}
	
	public function sendSettingAction(){
		$customerService = new Merchant_Service_Customer();
		$customerInfo = $customerService->getLoginInfo()->customer;
		$customer_id = $customerInfo['customer_id'];
		if(!$customer_id)return false;
		$am_is_email = $this->_request->getParam('am_is_email','');	//传回的值实际为 alert_messages 表的 am_id
		$am_is_alert = $this->_request->getParam('am_is_alert','');
		
		$am_config_id = $this->_request->getParam('am_config_id','');
		
		if($am_is_email  >0){
			$am_id = $am_is_email;
		}
		if($am_is_alert  >0){
			$am_id = $am_is_alert;
		}
		
		$data = array('ask'=>0);
		$cond = $setCond = array();
		$cond['customer_id'] = array('0',$customer_id);
		$cond['am_config_id'] = $am_config_id;
		$row = Merchant_Service_AlertMessages::getRowByCond($cond,'*','customer_id desc','all');
		if(count($row)>1){
			if($am_is_email  >0){
				if($row[0]['am_is_email']==1){
					$tmpArr = array('am_is_email'=>0);				
				}else{
					$tmpArr = array('am_is_email'=>1);				
				}
			}
			if($am_is_alert >0 ){
				if($row[0]['am_is_alert']==1){
					$tmpArr = array('am_is_alert'=>0);				
				}else{
					$tmpArr = array('am_is_alert'=>1);				
				}					
			}
			$tmpArr['am_last_update'] = date('Y-m-d H:i:s');
			$setCond = array('am_config_id'=>$am_config_id,'customer_id'=>$customer_id);
			$res = Merchant_Service_AlertMessages::updateByCond($tmpArr,$setCond);
			if($res){
				$data['ask'] = 1;
			}
		}elseif (count($row)==1){
			$insertRow = array();
			
			if($am_is_email  >0){	// 修改 am_is_email 字段的值
				if($row[0]['am_is_email']==1){
					$insertRow['am_is_email'] = 0;				
				}else{
					$insertRow['am_is_email'] = 1;				
				}
			}
			if($am_is_alert >0 ){	// 修改 am_is_alert 字段的值
				if($row[0]['am_is_alert']==1){
					$insertRow['am_is_alert'] = 0;
				}else{
					$insertRow['am_is_alert'] = 1;				
				}					
			}			
			
			$insertRow['am_config_id'] = $am_config_id;
			$insertRow['customer_id'] = $customer_id;
			$insertRow['et_id'] = $row[0]['et_id'];
			$insertRow['am_title'] = $row[0]['am_title'];
			$insertRow['am_level'] = $row[0]['am_level'];
			$insertRow['am_create_time'] = date('Y-m-d H:i:s');
			$res = Merchant_Service_AlertMessages::add($insertRow);
			if($res){
				$data['ask'] = 1;
			}
		}		
		die(json_encode($data));
	}
	
	public function setLevelAction(){
		$am_level = $this->_request->getParam('am_level','');	
		$am_config_id = $this->_request->getParam('am_config_id','');
		
		$customerService = new Merchant_Service_Customer();
		$customerInfo = $customerService->getLoginInfo()->customer;
		$customer_id = $customerInfo['customer_id'];
		if(!$customer_id)return false;
		
		$data = array('ask'=>0);
		$cond = $setCond = array();
		$cond['customer_id'] = array('0',$customer_id);
		$cond['am_config_id'] = $am_config_id;
		$row = Merchant_Service_AlertMessages::getRowByCond($cond,'*','customer_id desc','all');
		if(count($row)>1){
			$tmpArr = array('am_level'=>$am_level);
			$tmpArr['am_last_update'] = date('Y-m-d H:i:s');
			$setCond = array('am_config_id'=>$am_config_id,'customer_id'=>$customer_id);
			$res = Merchant_Service_AlertMessages::updateByCond($tmpArr,$setCond);
			if($res){
				$data['ask'] = 1;
			}
		}elseif (count($row)==1){
			$insertRow = array();
			$insertRow['am_config_id'] = $am_config_id;
			$insertRow['customer_id'] = $customer_id;
			$insertRow['et_id'] = $row[0]['et_id'];
			$insertRow['am_title'] = $row[0]['am_title'];
			$insertRow['am_level'] = $am_level;
			$insertRow['am_is_email'] = $row[0]['am_is_email'];
			$insertRow['am_is_alert'] = $row[0]['am_is_alert'];
			$insertRow['am_create_time'] = date('Y-m-d H:i:s');
			$res = Merchant_Service_AlertMessages::add($insertRow);
			if($res){
				$data['ask'] = 1;
			}
		}		
		die(json_encode($data));
		
	}
	
	
}
